package com.example.userclient.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.example.userclient.BookRemark
import com.example.userclient.HeartBeatInfo
import com.example.userclient.Main.ClientInfoHolder
import com.example.userclient.R
import com.example.userclient.activity.MessageKey
import com.example.userclient.databinding.ClientItemBinding
import com.example.userclient.databinding.MsgItemBinding

object NavigationAdapter : RecyclerView.Adapter<NavigationAdapter.ViewHolder>(){

    var dataSet: MutableList<HeartBeatInfo>? = null


    private var listener: OnItemClickListener?= null
    interface OnItemClickListener {
        fun onItemClick(itemView: View?, position: Int)
    }

    fun setOnItemClickListener(listener: NavigationAdapter.OnItemClickListener?) {
        NavigationAdapter.listener = listener
    }

    class ViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {

        private val mBinding: ClientItemBinding? = DataBindingUtil.bind(view)

        fun changeData(item: HeartBeatInfo) {
            if (mBinding != null) {
                mBinding.client = item
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NavigationAdapter.ViewHolder {
        val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.client_item, parent, false)
        return NavigationAdapter.ViewHolder(view)
    }

    override fun onBindViewHolder(holder: NavigationAdapter.ViewHolder, position: Int) {
        NavigationAdapter.dataSet?.let {
            holder.changeData(it.elementAt(position))
            holder.itemView.setOnClickListener{
                if (listener != null) {
                    listener!!.onItemClick(holder.itemView, holder.adapterPosition)
                }
            }
        }
    }

    override fun getItemCount(): Int {
        return dataSet?.size?:0
    }


}